/**
 * form混合
 * */
import { mapState, mapMutations } from 'vuex'
import { ApiRequest } from '@api/basePage'
export default {
    data () {
        return {
            dynamicData: {}, // 配置的数据
            sLoading: false,
            pagePath: '',
            grid: {
                xl: 8,
                lg: 8,
                md: 12,
                sm: 24,
                xs: 24
            },
            collapse: false, // 是否展开更多筛选条件
            formData: {}, // 表单提交数据
            copyFormData: {}, // copy form data 此处必须写
            copyFormDataRules: {}, // copy form rules 此处必须写
            requestApi: {}, // 页面所需接口集合
            uuid: '' // 唯一标识 访问页面必传
        }
    },
    computed: {
        ...mapState('admin/layout', ['isMobile']),
        ...mapState('admin/baseList', ['editPageData', 'tableRowData']),
        labelWidth () {
            return this.isMobile ? undefined : 120
        },
        labelPosition () {
            return this.isMobile ? 'top' : 'right'
        }
    },
    created () {
        this.uuid = this.$route.query.uuid
        this.getConfigData()
    },
    methods: {
        ...mapMutations('admin/baseList', ['setEditPageData']),
        // 获取基础信息
        getEditPageInfo () {
            let data = {
                uuid: this.uuid
            }
            // 开始请求信息
            ApiRequest(
                this.requestApi.getEditPageInfo.url,
                this.requestApi.getEditPageInfo.type,
                data,
                true
            ).then(res => {
                this.$refs.formData.resetFields() // 清空表单
                this.formData = res.retData
            })
        },
        // 组件内数据改变
        handleChangeFormData (data) {
            this.$set(this.$data, 'formData', data)
        },
        // 提交
        handleSubmit (name) {
            this.$refs[name].validate(valid => {
                if (valid) {
                    if (this.sLoading) {
                        return false
                    }

                    this.sLoading = true;
                    let data = this.formData
                    // 开始请求会员信息
                    ApiRequest(
                        this.requestApi.updatePageInfo.url,
                        this.requestApi.updatePageInfo.type,
                        data
                    ).then(res => {
                        if (res.retStatus === '1') {
                            this.$Message.success('操作成功!')
                            let jumpPath = this.dynamicData.basePath + 'List'
                            this.$router.push({
                                path: jumpPath
                            })
                        }
                        this.sLoading = false;
                    })
                } else {
                    this.$Message.error('请完善信息后再提交!')
                }
            })
        },
        // 清空表单
        handleReset (name) {
            this.$refs[name].resetFields();
            // this.getConfigData()
        },
        // 获取初始配置信息
        getConfigData () {
            this.pagePath = this.$route.path
            let baseEditConfig = require('@/configFile/product/baseEdit/index')
            let dynamicData = baseEditConfig.default[this.pagePath]
            this.requestApi = dynamicData.requestApi // 页面所需接口集合
            this.$set(this.$data, 'dynamicData', dynamicData)
            this.setEditPageData(dynamicData)

            // copy 一份数据 方便清空表单数据
            let copyFormData = JSON.parse(JSON.stringify(this.dynamicData.formData))
            let copyFormDataRules = JSON.parse(
                JSON.stringify(this.dynamicData.formDataRules)
            )
            // 传给子组件的数据
            this.copyFormData = copyFormData
            this.copyFormDataRules = copyFormDataRules

            // 重新获取数据
            this.uuid = this.$route.query.uuid
            this.getEditPageInfo()
        }
    },
    watch: {
        $route: 'getConfigData'
    }
}
